package net.aiform.template.action;

import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;

import net.aiform.account.pojo.Person;
import net.aiform.account.service.PersonService;
import net.aiform.bean.Constant;
import net.aiform.security.pojo.FormAccessController;
import net.aiform.template.pojo.Field;
import net.aiform.template.pojo.Item;
import net.aiform.template.pojo.Option;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class TemplateAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private PersonService personService;
	private TemplateService templateService;
	private HttpServletRequest req;
	private Person person;
	private String certPicCode;
	private String[] code;
	private String[] name;
	private String[] position;
	private String[] category;
	private String[] length;
	private String[] defaultValue;
	private String[] exampleValue;
	private String[] restrain;
	private String[] remark;
	private String[] hidden;
	private String random;
	private String recordUser;
	private String recordIp;
	private String recordTime;
	private Template template;
	private String template_recordAC;
	private String template_resultAC;
	private String template_statisticsAC;
	private String arg1;

	public String getTemplateList() {
		return "ssssssssssss";
	}

	public String saveNewTemplate() {
		template = initTemplate();
		this.templateService.save(template);
		return Action.SUCCESS;
	}

	private Template initTemplate() {
		Person p = (Person) req.getSession().getAttribute(Constant.LOGINUSER);
		this.template.setCreator(p.getId());
		this.template.setFormAccessControllers(new HashSet<FormAccessController>());
		this.template.getFormAccessControllers().add(getFAC(this.template_recordAC));
		this.template.getFormAccessControllers().add(getFAC(this.template_resultAC));
		this.template.getFormAccessControllers().add(getFAC(this.template_statisticsAC));
		initField();
		return this.template;
	}

	private void initField() {
		Field field;
		this.template.setFields(new HashSet<Field>());
		for (int i = 0; i < this.code.length; i++) {
			field = new Field();
			field.setTemplate(this.template);
			field.setCode(this.code[i]);
			field.setName(this.name[i]);
			field.setCategory(this.category[i]);
			field.setPosition(Integer.valueOf(this.position[i]));
			field.setLength(Integer.valueOf(this.length[i]));
			field.setDefaultValue(this.defaultValue[i]);
			field.setExampleValue(this.exampleValue[i]);
			field.setRestrain(this.restrain[i]);
			field.setRemark(this.remark[i]);
			field.setHidden(false);
			this.template.getFields().add(field);
		}
	}

	private FormAccessController getFAC(String category) {
		FormAccessController fac = new FormAccessController();
		fac.setCategory(category);
		fac.setTemplate(this.template);
		return fac;
	}

	public String buildNewTemplate() {
		//String code = req.getServletPath().substring(req.getServletPath().lastIndexOf("/") + 1);
		this.template = (Template) this.req.getSession().getAttribute(Constant.FORMTEMPLATE);
		String code = template.getCode();
		System.out.println(code);
		template.setFields(new HashSet<Field>());
		Field f1 = new Field();
		f1.setTemplate(template);
		f1.setOptions(new HashSet<Option>());
		//		f1.getOptions().add(new Option());
		template.getFields().add(f1);
		Field f2 = new Field();
		f2.setTemplate(template);
		Option o1 = new Option();
		o1.setField(f2);
		f2.setOptions(new HashSet<Option>());
		f2.getOptions().add(o1);
		o1.setItems(new HashSet<Item>());
		template.getFields().add(f2);
		//		template.setRecordAC(new FormAccessController());
		//		template.setResultAC(new FormAccessController());
		//		template.setStatiticsAC(new FormAccessController());
		this.templateService.save(template);
		return Action.SUCCESS;
	}

	public String designNewTemplate() {
		return Action.SUCCESS;
	}

	public String createNewTemplate() {
		log.info("create new template");
		//Person p = (Person) req.getSession().getAttribute(Constant.LOGINUSER);
		//if (!(p instanceof Person)) return Action.ERROR;
		this.template = templateService.getNewTemplate();
		//this.template.setCreator(p.getId());
		this.req.getSession().setAttribute(Constant.FORMTEMPLATE, this.template);
		return Action.SUCCESS;
	}

	public String execute() {
		return Action.SUCCESS;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getCertPicCode() {
		return certPicCode;
	}

	public void setCertPicCode(String certPicCode) {
		this.certPicCode = certPicCode;
	}

	public TemplateAction() {
		super();
	}

	public TemplateAction(PersonService personService, TemplateService templateService) {
		this.personService = personService;
		this.templateService = templateService;
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public String[] getCode() {
		return code;
	}

	public void setCode(String[] code) {
		this.code = code;
	}

	public String[] getName() {
		return name;
	}

	public void setName(String[] name) {
		this.name = name;
	}

	public String[] getPosition() {
		return position;
	}

	public void setPosition(String[] position) {
		this.position = position;
	}

	public String[] getCategory() {
		return category;
	}

	public void setCategory(String[] category) {
		this.category = category;
	}

	public String[] getLength() {
		return length;
	}

	public void setLength(String[] length) {
		this.length = length;
	}

	public String[] getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String[] defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String[] getExampleValue() {
		return exampleValue;
	}

	public void setExampleValue(String[] exampleValue) {
		this.exampleValue = exampleValue;
	}

	public String[] getRestrain() {
		return restrain;
	}

	public void setRestrain(String[] restrain) {
		this.restrain = restrain;
	}

	public String[] getRemark() {
		return remark;
	}

	public void setRemark(String[] remark) {
		this.remark = remark;
	}

	public String[] getHidden() {
		return hidden;
	}

	public void setHidden(String[] hidden) {
		this.hidden = hidden;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	public String getRecordUser() {
		return recordUser;
	}

	public void setRecordUser(String recordUser) {
		this.recordUser = recordUser;
	}

	public String getRecordIp() {
		return recordIp;
	}

	public void setRecordIp(String recordIp) {
		this.recordIp = recordIp;
	}

	public String getRecordTime() {
		return recordTime;
	}

	public void setRecordTime(String recordTime) {
		this.recordTime = recordTime;
	}

	public String getTemplate_recordAC() {
		return template_recordAC;
	}

	public void setTemplate_recordAC(String templateRecordAC) {
		template_recordAC = templateRecordAC;
	}

	public String getTemplate_resultAC() {
		return template_resultAC;
	}

	public void setTemplate_resultAC(String templateResultAC) {
		template_resultAC = templateResultAC;
	}

	public String getTemplate_statisticsAC() {
		return template_statisticsAC;
	}

	public void setTemplate_statisticsAC(String templateStatisticsAC) {
		template_statisticsAC = templateStatisticsAC;
	}
}
